<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0;margin: 0;}
        .container{
            width: 1226px;
            margin: 50px auto;
            display: flex;
            justify-content: space-between;
        }
        .container .magnifier-wrapper{
            width: 400px;
            height: 600px;
            /* background-color: orange; */
            position: relative;
        }

        .container .magnifier-wrapper .magnifier{
            width: 400px;
            height: 400px;
            background-color: rgba(0,0,0,0.5);
            position: relative;
        }
        .container .magnifier-wrapper .magnifier .shadow{
            width: 200px;
            height: 200px;
            background-color: rgba(0,0,0,0.5);
            position:absolute;
            left:0;
            top: 0;
        }

        .container .magnifier-wrapper .magnifier img{
            width: 100%;
            height: 100%;
        }

        .container .magnifier-wrapper .magnifier-list{
            width: 400px;
            height: 100px;
            background-color: rgba(0,0,0,0.5);
            margin-top: 20px;
        }

        .container .magnifier-wrapper .magnifier-preview{
            width: 500px;
            height: 500px;
            background-color: rgba(0,0,0,0.5);
            position: absolute;
            /* -webkit-calc(100% + 20px) 计算函数 =>在css中使用计算操作 */
            /* left: -webkit-calc(100% + 20px); */
            left: 420px;
            top: 0;
            /* overflow: hidden; */
        }

        .magnifier-preview img{
            width: 1000px;
            height: 1000px;
            position: relative;
            left:0;
            top:0;
            z-index: -1;
        }

        .container .item-info{
            flex: 1;
            margin-left: 20px;
            height: 600px;
            /* background-color: pink; */
        }
    </style>
</head>
<body>
    <!-- .container>.magnifier-wrapper>.magnifier+.magnifier-list+.magnifier-preview -->
    <div class="container">
        <!-- position:relative -->
        <div class="magnifier-wrapper">

            <!-- magnifier.offsetLeft 相对于父元素的 -->
            <div class="magnifier">
                <img class="smallImg" src="../images/girlsmall1.jpg" alt="">
                <div class="shadow"></div>
            </div>
            <div class="magnifier-list"></div>

            <!-- position:absolute -->
            <div class="magnifier-preview">
                <img class="bigImg" src="../images/girlbig1.jpg" alt="">
            </div>
        </div>
        <div class="item-info"></div>
    </div>


</body>
<script>

    /* 
    放大镜实现
    1. shadow遮住的区域,就是右边大盒子magnifier-preview中显示的区域  => 呈比例关系放大(布局相关)

        shadow(200)                smallImg(400)            x
    ——————————————————      ==    ——————————————   ==     —————— 
     magnifierPreview(500)          bigImg(1000)            X(=> -2.5x)
    
    2. 左边移动的是shadow，右边移动的是图片(图片 => 布局相关)

    3. shadow移动的距离和bigImg移动的距离, 也是等比例且反向的
    
    
    */



    var magnifierWrapper = document.getElementsByClassName("magnifier-wrapper")[0];
    var magnifier = document.getElementsByClassName("magnifier")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];

    var maxLeft = magnifier.clientWidth - shadow.clientWidth;
    var maxTop = magnifier.clientHeight - shadow.clientHeight;

    var scale = bigImg.clientWidth / smallImg.clientWidth; // 2.5
    console.log("scale",scale);



    magnifier.onmousemove = function(e){
        var e = e || window.event;

        var x = e.pageX - magnifierWrapper.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - magnifierWrapper.offsetTop - shadow.clientHeight / 2;

        if(x < 0) x = 0;
        if(x > maxLeft) x = maxLeft;

        if(y < 0) y = 0;
        if(y > maxTop) y = maxTop;
        
        // shadow移动
        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        // bigImg移动
        bigImg.style.left = -scale*x + "px";
        bigImg.style.top = -scale*y + "px";
    }



</script>
</html>